using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using APjunktion.Common;

namespace APNotifications
{
    public class Config
    {

        #region Public Methods

        public static NetworkCredential ProxyCredentials()
        {            
            if (ManualProxy())
            {
                NetworkCredential nc = new NetworkCredential();
                nc.UserName = UserName();
                nc.Password = Password();
                nc.Domain = Domain();
                return nc;
            }
            else
            {                
                NetworkCredential nc = new NetworkCredential();
                nc = (NetworkCredential)CredentialCache.DefaultCredentials;
                return nc;
            }
        }

        public static bool ManualProxy()
        {
            try
            {
                return bool.Parse(ConfigSettings.ReadSetting("ManualProxy"));
            }
            catch
            {
                return false;
            }
        }

        public static bool SoundOn()
        {
            try
            {
                return bool.Parse(ConfigSettings.ReadSetting("SoundOn"));
            }
            catch
            {
                return false;
            }
        }

        public static bool ShowNotifications()
        {
            try
            {
                return bool.Parse(ConfigSettings.ReadSetting("ShowNotifications"));
            }
            catch
            {
                return false;
            }
        }

        public static string UserName()
        {
            try
            {
                return ConfigSettings.ReadSetting("UserName");
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string Password()
        {
            try
            {
                return GetDecryptVal(ConfigSettings.ReadSetting("Password"));
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string Domain()
        {
            try
            {
                return ConfigSettings.ReadSetting("Domain");
            }
            catch
            {
                return string.Empty;
            }
        }

        public static int RefreshRate()
        {
            try
            {
                return Int32.Parse(ConfigSettings.ReadSetting("RefreshRate"));
            }
            catch
            {
                return 60000;
            }
        }

        public static int Transparency()
        {
            try
            {
                return Int32.Parse(ConfigSettings.ReadSetting("Transparency"));
            }
            catch
            {
                return 100;
            }
        }        

        public static string api_key()
        {
            return "a287872959b2dff889c6f27de764c265";
        }

        public static string secret()
        {
            return "05783a722038938ec652c8d26d98c301";
        }

        public static string ApplicationName()
        {
            try
            {
                return ConfigSettings.ReadSetting("ApplicationName");
            }
            catch
            {
                return string.Empty;
            }
        }

        public static decimal Version()
        {
            try
            {
                return decimal.Parse(ConfigSettings.ReadSetting("ApplicationVersion"));
            }
            catch
            {
                return 0;
            }
        }

        public static int UpdaterTimeout()
        {
            return 15000;
        }

        public static string UpdaterUrl()
        {
            if (ConfigSettings.ReadSetting("wsAPI").EndsWith("/"))
            {
                return ConfigSettings.ReadSetting("wsAPI") + "APjunktionInterface.asmx";
            }
            else
            {
                return ConfigSettings.ReadSetting("wsAPI") + "/APjunktionInterface.asmx";
            }
        }

        public static string SoundUnreadMess()
        {
            try
            {
                return ConfigSettings.ReadSetting("Soundunread_message");
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string SoundPoke()
        {
            try
            {
                return ConfigSettings.ReadSetting("Soundunread_poke");
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string SoundFriendReq()
        {
            try
            {
                return ConfigSettings.ReadSetting("Soundfriend_invite");
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string SoundGroupInv()
        {
            try
            {
                return ConfigSettings.ReadSetting("Soundgroup_invite");
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string SoundEventInv()
        {
            try
            {
                return ConfigSettings.ReadSetting("Soundevent_invite");
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string GetSoundToPlay(string key)
        {
            try
            {
                if (ConfigSettings.ReadSetting(key) == string.Empty)
                {
                    return "notify.wav";
                }
                else
                {
                    return ConfigSettings.ReadSetting(key);
                }
                
            }
            catch
            {
                return "notify.wav";
            }
        }

        public static void SaveManualProxy(bool ManualProxy)
        {
            ConfigSettings.WriteSetting("ManualProxy", ManualProxy.ToString());
        }

        public static void SaveSoundOn(bool SoundOn)
        {
            ConfigSettings.WriteSetting("SoundOn", SoundOn.ToString());
        }

        public static void SaveShowNotifications(bool ShowNotifications)
        {
            ConfigSettings.WriteSetting("ShowNotifications", ShowNotifications.ToString());
        }

        public static void SaveNetworkCredentials(string UserName, string Password, string Domain)
        {
            ConfigSettings.WriteSetting("UserName", UserName);
            ConfigSettings.WriteSetting("Password", GetEncryptVal(Password));
            ConfigSettings.WriteSetting("Domain", Domain);
        }

        public static void SaveRefreshRate(int RefreshRate)
        {
            ConfigSettings.WriteSetting("RefreshRate", RefreshRate.ToString());
        }

        public static void SaveTransparency(int Transparency)
        {
            ConfigSettings.WriteSetting("Transparency", Transparency.ToString());
        }

        public static void SaveSoundUnreadMess(string SoundUnreadMess)
        {
            ConfigSettings.WriteSetting("Soundunread_message", SoundUnreadMess.ToString());
        }

        public static void SaveSoundPoke(string SoundPoke)
        {
            ConfigSettings.WriteSetting("Soundunread_poke", SoundPoke.ToString());
        }

        public static void SaveSoundFriendReq(string SoundFriendReq)
        {
            ConfigSettings.WriteSetting("Soundfriend_invite", SoundFriendReq.ToString());
        }

        public static void SaveSoundGroupInv(string SoundGroupInv)
        {
            ConfigSettings.WriteSetting("Soundgroup_invite", SoundGroupInv.ToString());
        }

        public static void SaveSoundEventInv(string SoundEventInv)
        {
            ConfigSettings.WriteSetting("Soundevent_invite", SoundEventInv.ToString());
        }

        #endregion

        #region Private Methods

        private static string GetDecryptVal(string EncryptText)
        {
            return EncryptDecrypt.Decrypt(EncryptText, getK());
        }

        private static string GetEncryptVal(string DecryptText)
        {
            return EncryptDecrypt.Encrypt(DecryptText, getK());
        }        

        private static string getK()
        {
            return "asdf0987";
        }

        #endregion
    }
}
